from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from django.core.mail import mail_admins
from django.contrib import messages

from .forms import FeedbackForm


def feedback(request):
    """
    收集来自用户的反馈信息
    """
    if request.method == 'POST':
        form = FeedbackForm(data=request.POST)
        if form.is_valid():
            template = render_to_string('emails/feedback.txt', {
                'name': form.cleaned_data['name'],
                'email': form.cleaned_data['email'],
                'comments': form.cleaned_data['comments'],
            })
            mail_admins('2buntu Feedback', template)
            messages.success(request, "谢谢你的反馈。你的信息已成功发送给管理员")
            return redirect('translations:index')
    else:
        form = FeedbackForm()
    return render(request, "feedback.html", {
        'form': form,
    })
